﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CiroGoruntuleme
{
    public partial class frmMain : Form
    {
        Mikro mk = new Mikro();
        List<Depo> depolar;
        List<Depo> seciliDepolar = new List<Depo>();
        DateTime tarih;

        public frmMain()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!backgroundWorker1.IsBusy)
            {
                tarih = dteTarih.DateTime;
                seciliDepolar.Clear();
                foreach (Depo item in depolar)
                {
                    if (secilimi(item.Adi))
                        seciliDepolar.Add(item);
                }
                timer1.Enabled = false;
                backgroundWorker1.WorkerSupportsCancellation = true;
                backgroundWorker1.RunWorkerAsync();
            }
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            List<Rapor> rapor = new List<Rapor>();
            foreach (Depo item in seciliDepolar)
            {
                rapor.Add(mk.OncekiGunSatisi(new DateTime(tarih.Year, tarih.Month, tarih.Day), item));
            }
            e.Result = rapor;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            timer1.Enabled = true;
            if (e.Result != null)
            {
                gridControl1.DataSource = e.Result;
                for (int i = 3; i < 6; i++)
                {
                    gridView1.Columns[i].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                    gridView1.Columns[i].DisplayFormat.FormatString = "n2";
                }
            }
        }

        private bool secilimi(string adi)
        {
            for (int i = 0; i < cmbDepo.Properties.Items.Count; i++)
            {
                if (cmbDepo.Properties.Items[i].CheckState == CheckState.Checked)
                {
                    if (cmbDepo.Properties.Items[i].Value == adi)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            dteTarih.DateTime = DateTime.Now;
            depolar = new Mikro().ListDepo();
            foreach (Depo item in depolar)
            {
                cmbDepo.Properties.Items.Add(item.Adi, false);
            }
        }
    }
}
